package as3.component.support
{
	import flash.events.Event;
	import flash.geom.Point;
	
	import as3.component.Component;
	import as3.component.manager.PopupManager;

	public class Modellayer extends Component
	{
		private var list:Array = [0, 0.2, 0.4, 0.6, 0.8, 1];
		private var index:int = 0;
		
		public function Modellayer()
		{
			draw();
		}
		
		protected function draw():void
		{
			graphics.clear();
			graphics.beginFill(0, 0.7);
			graphics.drawRect(0,0,10,10);
			graphics.endFill();
			alpha = 0;
		}
		
		private var _isOpen:Boolean;

		/**是否打开*/
		public function get isOpen():Boolean
		{
			return _isOpen;
		}

		/**
		 * @private
		 */
		public function set isOpen(value:Boolean):void
		{
			_isOpen = value;
		}

		
		public function show():void
		{
			removeEventListener(Event.ENTER_FRAME, onCloseEnter);
			addEventListener(Event.ENTER_FRAME, onShowEnter);
			//PopupManager.getInstance().addPopUp(this, new Point(FaceValues.FACE_X,FaceValues.FACE_Y));
			isOpen = true;
			stage.addEventListener(Event.RESIZE,onResizeHandler,false,0,true);
		}
		
		private function onResizeHandler(e:Event):void
		{
		}
		
		private function onShowEnter(evt:Event):void
		{
			if (index > 5) {
				alpha = 1;
				index = 5;
				removeEventListener(Event.ENTER_FRAME, onShowEnter);
				dispatchEvent(new Event(Event.OPEN));
				return;
			}
			alpha = list[index];
			index++;
		}
		
		public function close():void
		{
			removeEventListener(Event.ENTER_FRAME, onShowEnter);
			addEventListener(Event.ENTER_FRAME, onCloseEnter);
			isOpen = false;
			if(stage)
				stage.removeEventListener(Event.RESIZE,onResizeHandler);
		}
		
		private function onCloseEnter(evt:Event):void
		{
			if (index < 0) {
				alpha = 0;
				index = 0;
				removeEventListener(Event.ENTER_FRAME, onCloseEnter);
				PopupManager.getInstance().removePopUp(this);
				return;
			}
			alpha = list[index];
			index--;
		}
		
	}
}